<?php
/**
 * News page controller
 *
 * @author Dima
 *
 */

class FacebookController extends modules_default_controllers_ControllerBase
{
//	protected $_bForLoggedUsersOnly = true;
	protected $_bForLoggedUsersOnly = false;
	protected $_contentLayout = 'contentonly';
	
	public $ajaxable = array(
	);


//----------------------------------------------------------------------------------------------------


	public function init()
	{
		parent::init();


		if (! $this->getRequest()->isXmlHttpRequest())
		{
			$this->view->title = 'Facebook';
		}

		$this->_helper->AjaxContext()->initContext('json');
	}

//----------------------------------------------------------------------------------------------------

	public function indexAction()
	{
//		$this->_helper->viewRenderer->setNoRender();
//		$this->_helper->layout->disableLayout();

		$userId = $user = Lemar_Facebook::instance()->get_loggedin_user();
		if (!$user)
		{
			Lemar_Facebook::instance()->redirect(Lemar_Facebook::instance()->get_login_url('http://qukkie.winkyweb.dyndns.org/facebook/', 1, 'user_birthday, friends_birthday'));
		}
        try
        {
            if (!$authUserId = Lemar_Facebook::instance()->api_client->users_getLoggedInUser()) {
            	die(1);
//            	$facebook->redirect(Lemar_Facebook::instance()->get_add_url());
            }
        } catch (Exception $ex)
         {
			Lemar_Facebook::instance()->clear_cookie_state();
			$this->_redirect('/facebook?'. rand());
        }
		$userInfo = Lemar_Facebook::instance()->api_client->fql_query("SELECT uid, name, profile_url, birthday_date FROM user WHERE uid IN (SELECT uid2 FROM friend WHERE uid1=$userId)");
		print_r($userInfo);
	}

//----------------------------------------------------------------------------------------------------

	public function ololoAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$this->_helper->layout->disableLayout();
		echo '<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script>';
		echo '<p>Это простое приложения для просмотра списка ваших друзей на Facebook. Жмите кнопку внизу, разрешайте приложению доступ к Facebook, и смотрите список друзей.</p>

<p><fb:login-button onlogin="javascript: window.location = \'/facebook/\';"/></p>';

		echo '<script type="text/javascript">
            FB.init("119466001408556", "/facebook_xdreceiver.html");
    </script>';

	}

//----------------------------------------------------------------------------------------------------

}